<?php

class Mailer_task extends Controller {

	function Mailer_task() {
		parent::Controller();
		$this->load->library('xmlexcel');
		$this->load->model('model_mailer_task');
		if (!$this->usersession->userLoggedIn()) {
			$aResponse = array("error" => true, "description" => "Sesiunea a expirat", "type" => "nosession");
			die(json_encode($aResponse));
		}
	}


	function bringTask() {

		$table = 'mailer_task';
		$s_limit = $this->input->post("start");
		$e_limit = $this->input->post("limit");
		$iMailerId = $this->input->post("mailer_id");
		
		$aResponse = $this->model_mailer_task->bringTask($table, $s_limit, $e_limit, $iMailerId);
		echo json_encode($aResponse);
	}

	/**
	 * Functie de add task ( verificare data)
	 */
	function addTask() {

		$data["mailer_task_date"] = $this->input->post("mailer_task_date");
		$data["mailer_id"] = $this->input->post("mailer_id");

		$bResult = $this->model_mailer_task->addTask($data);
		if (!$bResult) {
			$aResponse = array("error" => true, "description" => "Mai exista aceasta data", "type" => "data");
			echo json_encode($aResponse);
			return;
		}

		$aResponse = array("error" => false, "description" => "Taskul a fost adaugat", "success" => true);
		echo json_encode($aResponse);
	}

	function deleteTask() {
		$result = $this->model_mailer_task->deleteTask($_POST);
		$aResponse = array("error" => false, "description" => "Datele au fost sterse", "success" => true);
		echo json_encode($aResponse);
	}

	function delayTask() {
		$result = $this->model_mailer_task->delayTask($_POST);
		$aResponse = array("error" => false, "description" => "Datele au fost salvate", "success" => true);
		echo json_encode($aResponse);
	}

	/**
	 * functie care seteaza play/pause pe un task 0 play, 1 pause
	 */
	function operationTask() {
		$result = $this->model_mailer_task->operationTask($_POST);
		$aResponse = array("error" => false, "description" => "Datele au fost salvate", "success" => true);
		echo json_encode($aResponse);
	}

	/**
	 * functie care retransmite un task ( se pune status 0 la task si in tabelul mailer_report se modifica statusurile)
	 */
	function resendTask() {
		$this->model_mailer_task->operationTask($_POST);
		$this->model_mailer_task->resendTask($_POST);
		$aResponse = array("error" => false, "description" => "Datele au fost salvate", "success" => true);
		echo json_encode($aResponse);
	}

	/**
	 * functie care sincronizeaza accesul pe linkuri
	 */
	/*function syncLink() {
		//set POST variables
		$url = $this->config->item("mtp_satelit_return");
		$fields = array('lname' => "mumu");

		//url-ify the data for the POST
		foreach ($fields as $key => $value) {
			$fields_string .= $key . '=' . $value . '&';
		}
		rtrim($fields_string, '&');

		//open connection
		$ch = curl_init();

		//set the url, number of POST vars, POST data
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, count($fields));
		curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

		//execute post
		$result = curl_exec($ch);
		//close connection
		curl_close($ch);
		$result = json_decode($result, true);
		//print_r($result);

		foreach ($result as $record) {
			$query = "INSERT INTO mailer_link_report (id_satelit,link_id,link_nume,link_data_acces ,client_person_id) VALUES (?,?,?,?,?)  ON DUPLICATE KEY UPDATE id_satelit=?;";
			$this->db->query($query, array(
			                              $record["id"], $record["link_id"], $record["link_nume"],
			                              $record["link_data_acces"], $record["client_person_id"], $record["id"]));
		}
		$aResponse = array("error" => false, "description" => "Datele au fost sincronizate", "success" => true);
		echo json_encode($aResponse);
	}
*/

	/** MINI RAPORT**/
	function bringReport() {

		$table = 'mailer_report';
		$s_limit = $this->input->post("start");
		$e_limit = $this->input->post("limit");
		$dir = $this->input->post("dir");
		$sort = $this->input->post("sort");
		$iMailerTaskId = $this->input->post("mailer_task_id");

		if ($iMailerTaskId == "") {
			$iMailerTaskId = 0;
		}

		echo json_encode($this->model_mailer_task->bringReport($table, $s_limit, $e_limit, $sort, $dir, $iMailerTaskId));
	}

	function editContact() {
		$this->load->model("model_campaign");
		$result = $this->model_mailer_task->editContact($_POST);

		if (!$result) {
			$aResponse = array(
				"error" => true,
				"description" => "Incercati sa modificati o adresa de email pe care deja s-a trimis newsletter..",
				"type" => "data", "success" => true);
			echo json_encode($aResponse);
			return;
		}


		$result = $this->model_campaign->editContact($_POST);

		$aResponse = array(
			"error" => false, "description" => "Operatia a fost efectuata", "type" => "data", "success" => true);
		if (!$result) {
			$aResponse = array("error" => true, "description" => "Operatia NU a fost efectuata! ", "type" => "data");
		}
		echo json_encode($aResponse);
	}


	function export() {

		$table = 'mailer_report';
		$s_limit = 0;
		$e_limit = 66000;
		$dir = "asc";
		$sort = "lname";
		$iMailerTaskId = $this->uri->segment(3);
		$customs["sex"] = $this->config->item("sexContact");

		$aResult = $this->model_mailer_task->bringReport($table, $s_limit, $e_limit, $sort, $dir, $iMailerTaskId);

		$litere_er = array('"', '<', '>', "/");
		$litere_sol = array(",", " ", " ", "");

		header("Content-type: application/vnd.ms-excel;charset=utf-8");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Content-disposition:  attachment; filename=raport.xls");
		$this->load->library('xmlexcel');
		$newexcel = new Xmlexcel;

		$newexcel->write_header();
		$newexcel->write_document_properties('MTP');
		$newexcel->write_styles();

		$newexcel->start_worksheet();
		$newexcel->write_row('
		<Column ss:Width="100"/>
		<Column ss:Width="100"/>
		<Column ss:Width="50"/>
		<Column ss:Width="150"/>
		<Column ss:Width="300"/>
		<Column ss:Width="100"/>
		<Column ss:Width="100"/><Column ss:Width="100"/>
		<Column ss:Width="100"/><Column ss:Width="100"/>
		<Column ss:Width="100"/>');

		$aTaskStatistic = $this->model_mailer_task->getTaskStatistic($iMailerTaskId);
		$newexcel->write_row('
		<Row>		   
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Abonati</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Email 1 de trimis</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Email 2 de trimis</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Total de trimis</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Email 1 trimis succes</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Email 2 trimis succes</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Email 1 erori</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Email 2 erori</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Emailuri ramase de trimis</Data></Cell>		
		</Row>');
		$newexcel->write_row('
		<Row>		   
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">'.$aTaskStatistic["total_contacte"].'</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">'.$aTaskStatistic["total_email1"].'</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">'.$aTaskStatistic["total_email2"].'</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">'.$aTaskStatistic["total_de_trimis"].'</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">'.$aTaskStatistic["email1_trimis_succes"].'</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">'.$aTaskStatistic["email2_trimis_succes"].'</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">'.$aTaskStatistic["email1_netrimis"].'</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">'.$aTaskStatistic["email2_netrimis"].'</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">'.$aTaskStatistic["email_ramas"].'</Data></Cell>		
		</Row>');
		
		

		$newexcel->write_row('
		<Row>		   
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Nume</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Prenume</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Titlu</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Email</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Companie</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Descriere</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Data Trimitere</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Creat de</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">Modififcat de</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">ID Baza de date</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">NUME TRIMIS EMAIL</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">PRENUME TRIMIS EMAIL</Data></Cell>
		 <Cell ss:StyleID="headerStyle"><Data ss:Type="String">SUBIECT PERSONALIZAT</Data></Cell>
		</Row>');


		foreach ($aResult['DS_REPORT'] as $key => $row) {

			if ($row["email"] != "") {
				$newexcel->write_row('<Row>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["lname"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["fname"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $customs["sex"][$row["sex"]] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' .str_replace($litere_er, $litere_sol, $row["email"]) . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["company"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . str_replace($litere_er, $litere_sol, $row["descriere"]) . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["send_date"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["create_by"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["modify_by"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["person_id"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["nume_personalizat"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["prenume_personalizat"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["subiect_personalizat"] . '</Data></Cell>
		    </Row>');
			}
			if ($row["email2"] != "") {
				$newexcel->write_row('<Row>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["lname"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["fname"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $customs["sex"][$row["sex"]] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' .str_replace($litere_er, $litere_sol, $row["email2"]) . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["company"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . str_replace($litere_er, $litere_sol, $row["descriere2"]) . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["send_date2"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["create_by"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["modify_by"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["person_id"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["nume_personalizat"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["prenume_personalizat"] . '</Data></Cell>
		    <Cell ss:StyleID="defaultComp"><Data ss:Type="String">' . $row["subiect_personalizat"] . '</Data></Cell>
		    </Row>');
			}
		}

		$newexcel->end_worksheet();
		$newexcel->write_footer();
		print $newexcel->output();
		exit;

	}


	function exportLink() {


		$iMailerTaskId = $this->uri->segment(3);
		$iMailer = $this->db->get_where("mailer_task", array("mailer_task_id" => $iMailerTaskId))->row()->mailer_id;

		header("Content-type: application/vnd.ms-excel;charset=utf-8");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Content-disposition:  attachment; filename=raport.xls ");
		$newexcel = new Xmlexcel;
		$newexcel->write_header();
		$newexcel->write_document_properties('MTP');
		$newexcel->write_styles();
		$newexcel->start_worksheet("Sumar");


		//setare lungime coloane
		for ($i = 0; $i <= 5; $i++) {
			$sCell .= '<Column ss:Width="150"/>';
		}
		$newexcel->write_row($sCell);


		$newexcel->end_worksheet();
		$newexcel->write_footer();


		$aLink = $this->db->get_where("mailer_link",array("mailer_id"=>$iMailer))->result_array() ;

		foreach($aLink as $aRec) {
			$aRapLink .= $this->linkContacteUnice($aRec["link_id"],$aRec["link_cod"]);
		}


		print  $newexcel->header . $newexcel->docprop . $newexcel->styles . $this->linkSumar($iMailer) .  $aRapLink.$newexcel->footer;
		exit;

	}



	function linkContacteUnice($iLink,  $sLinkNume) {

		$customs["age"] = $this->config->item("ageContact");
		$customs["income"] = $this->config->item("incomeContact");
		$customs["employees"] = $this->config->item("employeeContact");
		$customs["available"] = $this->config->item("availableContact");
		$customs["priority"] = $this->config->item("priorityContact");
		$customs["sex"] = $this->config->item("sexContact");
		$customs["turnover"] = $this->config->item("turnoverContact");

		$sSql = " SELECT count(mailer_link.link_id) as nr_click,  link_data_acces,  mailer_link.link_nume, `domain`.name as domain_name,   `SD`.name as subdomain_name,
              division.name as division_name, persons.address, persons.address2, birth, age, income, turnover, employees, cui,   persons.person_id,

            IF(extern =1 , client_persons.company , persons.company) as company,
            IF(extern =1 , client_persons.lname , persons.lname) as lname,
            IF(extern =1 , client_persons.fname , persons.fname) as fname,
            IF(extern =1 , CP.name , P.name) as position_name,
            IF(extern =1 , CSP.name , SP.name) as subposition_name,
            IF(extern =0 , persons.phone , client_persons.phone) as phone,
            IF(extern =0 , persons.mobil , client_persons.mobil) as mobil,
            IF(extern =1 , client_persons.email , persons.email) as email,
            IF(extern =1 , client_persons.email2 , persons.email2) as email2,
            IF(extern =1 , CC.name , city.name) as city_id_val,
            IF(extern =1 , client_persons.city_id , persons.city_id) as city_id,
            IF(extern =1 , CCo.name , county.name) as county_id_val,
            IF(extern =1 , client_persons.sex , persons.sex) as sex,
            IF(extern =1 , client_persons.web , persons.web) as web
            from mailer_link
                left join mailer_link_report on mailer_link_report.link_id=mailer_link.link_id
                left join client_persons on client_persons.client_persons_id=mailer_link_report.client_person_id
                left join persons on persons.person_id = client_persons.person_id
                left join position P on P.position_id=persons.position_id
                left join position CP on CP.position_id=client_persons.position_id
                left join position SP on SP.position_id=persons.position_sub_id
                left join position CSP on CSP.position_id=client_persons.position_sub_id
                left join city  on city.city_id=persons.city_id
                left join county  on county.county_id=persons.county_id
                left join city CC  on CC.city_id=client_persons.city_id
                left join county CCo on CCo.county_id=client_persons.county_id
                left join tele_status  on tele_status.ts_id=client_persons.ts_id
                left join `domain`  on `domain`.domain_id=persons.domain_id
                left join `domain` SD  on `SD`.domain_id=persons.domain_sub_id
                left join `division`   on `division`.division_id=persons.division_id
			where mailer_link.link_id = {$iLink} GROUP BY client_person_id
            order by link_data_acces desc , lname asc ";
		$aLinkResult = $this->db->query($sSql)->result_array();

		$newexcel = new Xmlexcel;
		$newexcel->start_worksheet($sLinkNume);

		//setare lungime coloane
		for ($i = 0; $i <= 25; $i++) {
			$sCell .= '<Column ss:Width="200"/>';
		}
		$newexcel->write_row($sCell);

		$newexcel->write_row('
        <Row>
			<Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Nr acces</Data></Cell>
			<Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Compania</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Nume</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Prenume</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Sex</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Domeniu</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Subdomeniu</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Functia</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Subfunctia</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Departament</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Adresa</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Cod postal</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Oras</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Judet</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Telefon</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Mobil</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Email</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Email 2</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Web</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Zi nastere</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Grupa varsta</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Grupa venit</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Cifra afaceri</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Nr angajati</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Cod inregistrare</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">ID baza de date</Data></Cell>

        </Row>');

		foreach ($aLinkResult as $key => $aContactRec) {

		    $newexcel->write_row('<Row>
		             <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["nr_click"] . '</Data></Cell>
		            <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["company"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["lname"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["fname"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $customs["sex"][$aContactRec["sex"]] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["domain_name"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["subdomain_name"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["position_name"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["subposition_name"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["division_name"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["address"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["address2"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["city_id_val"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["county_id_val"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["phone"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["mobil"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["email"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["email2"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["web"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["birth"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $customs["age"][$aContactRec["age"]] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $customs["income"][$aContactRec["income"]] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $customs["turnover"][$aContactRec["turnover"]] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $customs["employees"][$aContactRec["employees"]] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["cui"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["person_id"] . '</Data></Cell>
		    </Row>');
		}
		$newexcel->end_worksheet();
		return $newexcel->ws . $newexcel->rows . $newexcel->we;
	}



	function linkSumar($iMailer) {

		$customs["age"] = $this->config->item("ageContact");
		$customs["income"] = $this->config->item("incomeContact");
		$customs["employees"] = $this->config->item("employeeContact");
		$customs["available"] = $this->config->item("availableContact");
		$customs["priority"] = $this->config->item("priorityContact");
		$customs["sex"] = $this->config->item("sexContact");
		$customs["turnover"] = $this->config->item("turnoverContact");

		$sSql = " SELECT link_data_acces,  mailer_link.link_nume, `domain`.name as domain_name,   `SD`.name as subdomain_name,
              division.name as division_name, persons.address, persons.address2, birth, age, income, turnover, employees, cui,    persons.person_id ,

            IF(extern =1 , client_persons.company , persons.company) as company,
            IF(extern =1 , client_persons.lname , persons.lname) as lname,
            IF(extern =1 , client_persons.fname , persons.fname) as fname,
            IF(extern =1 , CP.name , P.name) as position_name,
            IF(extern =1 , CSP.name , SP.name) as subposition_name,
            IF(extern =0 , persons.phone , client_persons.phone) as phone,
            IF(extern =0 , persons.mobil , client_persons.mobil) as mobil,
            IF(extern =1 , client_persons.email , persons.email) as email,
            IF(extern =1 , client_persons.email2 , persons.email2) as email2,
            IF(extern =1 , CC.name , city.name) as city_id_val,
            IF(extern =1 , client_persons.city_id , persons.city_id) as city_id,
            IF(extern =1 , CCo.name , county.name) as county_id_val,
            IF(extern =1 , client_persons.sex , persons.sex) as sex,
            IF(extern =1 , client_persons.web , persons.web) as web
            from mailer_link
                left join mailer_link_report on mailer_link_report.link_id=mailer_link.link_id
                left join client_persons on client_persons.client_persons_id=mailer_link_report.client_person_id
                left join persons on persons.person_id = client_persons.person_id
                left join position P on P.position_id=persons.position_id
                left join position CP on CP.position_id=client_persons.position_id
                left join position SP on SP.position_id=persons.position_sub_id
                left join position CSP on CSP.position_id=client_persons.position_sub_id
                left join city  on city.city_id=persons.city_id
                left join county  on county.county_id=persons.county_id
                left join city CC  on CC.city_id=client_persons.city_id
                left join county CCo on CCo.county_id=client_persons.county_id
                left join tele_status  on tele_status.ts_id=client_persons.ts_id
                left join `domain`  on `domain`.domain_id=persons.domain_id
                left join `domain` SD  on `SD`.domain_id=persons.domain_sub_id
                left join `division`   on `division`.division_id=persons.division_id
			where mailer_id = {$iMailer}
            order by link_data_acces desc , lname asc ";
		$aLinkResult = $this->db->query($sSql)->result_array();

		$newexcel = new Xmlexcel;
		$newexcel->start_worksheet("Sumar");

		//setare lungime coloane
		for ($i = 0; $i <= 25; $i++) {
			$sCell .= '<Column ss:Width="200"/>';
		}
		$newexcel->write_row($sCell);

		$newexcel->write_row('
        <Row>
			<Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Data acces</Data></Cell>
			<Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Link</Data></Cell>
			<Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Compania</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Nume</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Prenume</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Sex</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Domeniu</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Subdomeniu</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Functia</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Subfunctia</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Departament</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Adresa</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Cod postal</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Oras</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Judet</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Telefon</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Mobil</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Email</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Email 2</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Web</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Zi nastere</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Grupa varsta</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Grupa venit</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Cifra afaceri</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Nr angajati</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">Cod inregistrare</Data></Cell>
            <Cell ss:StyleID="teleLogDayTitleColumn"><Data ss:Type="String">ID baza de date</Data></Cell>

        </Row>');

		foreach ($aLinkResult as $key => $aContactRec) {

		    $newexcel->write_row('<Row>
		             <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["link_data_acces"] . '</Data></Cell>
		              <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["link_nume"] . '</Data></Cell>
		            <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["company"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["lname"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["fname"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $customs["sex"][$aContactRec["sex"]] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["domain_name"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["subdomain_name"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["position_name"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["subposition_name"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["division_name"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["address"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["address2"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["city_id_val"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["county_id_val"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["phone"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["mobil"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["email"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["email2"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["web"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["birth"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $customs["age"][$aContactRec["age"]] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $customs["income"][$aContactRec["income"]] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $customs["turnover"][$aContactRec["turnover"]] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $customs["employees"][$aContactRec["employees"]] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["cui"] . '</Data></Cell>
                    <Cell ss:StyleID="teleLogChestionarTxt"><Data ss:Type="String">' . $aContactRec["person_id"] . '</Data></Cell>
		    </Row>');
		}
		$newexcel->end_worksheet();
		return $newexcel->ws . $newexcel->rows . $newexcel->we;
	}

}



/* End of file Mailer_task.php */
/* Location: ./system/application/controllers/admin/Mailer_task.php */